package swingxrcp.framework.client.swingx;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;

import javax.swing.JDialog;

import swingxrcp.framework.client.application.Application;
import swingxrcp.framework.client.context.SwingXContext;
import swingxrcp.framework.util.log.Logger;
import swingxrcp.framework.util.log.LoggerFactory;

public class JXDialog extends JDialog implements SwingX {
	/**
	 * 日志输出接口
	 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	private boolean running = false;

	/**
	 * 默认构造方法
	 */
	public JXDialog() {
		super();
	}

	public JXDialog(Frame owner, String title, boolean modal) {
		super(owner, title, modal);
	}

	public JXDialog(Dialog owner, String title, boolean modal) {
		super(owner, title, modal);
	}

	public JXFrame getMainFrame() {
		return Application.mainFrame;
	}

	// TODO SwingX生命周期 -=================================================================
	public void open() {
		running = true;
		setVisible(running);
	}

	public void close() {
		running = false;
		removeAttributes();
		dispose();
	}

	public boolean isRunning() {
		return running;
	}

	// TODO 处理Action事件 -=================================================================

	public void registerAction(Component component, String method) {
		if (component != null) {
		} else {
			logger.error("为组件添加Action事件驱动器,组件还没有初始化!  方法名:" + method);
		}
	}

	// TODO 处理Thread事件 -=================================================================
	/**
	 * 激活当前UI容器
	 */
	public void pageActivated() {

	}

	/**
	 * 执行先/后回调
	 */
	public void executeCallback() {

	}

	/**
	 * 前执行
	 */
	public void executeBefore() {
		/* *** 子类实现 *** */
	}

	/**
	 * 后执行
	 *
	 */
	public void executeAfter() {
		/* *** 子类实现 *** */
	}

	public void run() {
		// nothing
	}

	// TODO 处理缓存 -=================================================================
	public Object getAttribute(String key) {
		return SwingXContext.get(this.getClass().getName(), key);
	}

	public void setAttribute(String key, Object object) {
		SwingXContext.add(this.getClass().getName(), key, object, false);
	}

	public void setAttribute(String key, Object object, boolean lock) {
		SwingXContext.add(this.getClass().getName(), key, object, lock);
	}

	public void removeAttribute(String key) {
		SwingXContext.remove(this.getClass().getName(), key);
	}
	
	public void removeAttributes(){
		SwingXContext.removeCacheById(this.getClass().getName());
	}
}
